package scribble;

import java.awt.*;
import java.util.Vector;

import javax.swing.*;

public class ScribbleCanvas extends JComponent {
	protected Image im;
	protected int width, height;
	protected Graphics offScreen;
	protected Color penColor = Color.black;
	protected  boolean mouseButtonDown = false;
	protected int x,y;
	
	protected int n_Columns=3;
	protected int n_Rows=3;
	//protected boolean grid=true;

	
	
	public ScribbleCanvas(){
		setBackground(Color.white);
		
	}
	
	public void update(Graphics g){
		drawGrid();
		g.drawImage(im, 0, 0, this);
	}
	
	public void paint(Graphics g){
		
		update(g);
	}
	
	public Graphics getOffScreenGraphics(){
		
		return offScreen;
	}
	
	public void setPenColor(Color c){
		penColor = c;
		offScreen.setColor(penColor);
	}
	
	public void clearCanvas(){
		offScreen.setColor(Color.white);
		offScreen.fillRect(0, 0, width, height);
		offScreen.setColor(penColor);		
	}
	
	public void setBounds(int x, int y, int width, int height){
		this.width=width;
		this.height=height;
		Image newIm = createImage(width, height);
		offScreen = newIm.getGraphics();
		clearCanvas();
		if (im != null) {
			offScreen.drawImage(im, 0, 0, this);
		}
		im = newIm;
		super.setBounds(x, y, width, height);

		repaint();
	}

	
	private void drawGrid(){
		float col_Step, row_Step;
		int i,j;
		
		col_Step=width/n_Columns;
		row_Step=height/n_Rows;
		offScreen.setColor(Color.darkGray);	
		
		
			//draw columns
			for (i = 1 ; i < n_Columns; i++) {
				offScreen.drawLine((int)col_Step*i, 0, (int)col_Step*i, height);
			}
			//draw lines
			for (j = 1; j< n_Rows; j++) {
				offScreen.drawLine(0, (int)row_Step*j, width, (int)row_Step*j);
			}
		
		
	}
	
	
}
